package com.caiheng.api.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.product.OrderAppraise;
import com.caiheng.api.entity.product.OrderAppraiseImg;
import com.caiheng.api.entity.product.ProductAttrValue;
import com.caiheng.api.entity.product.ProductOrder;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.product.OrderAppraiseService;
import com.vdurmont.emoji.EmojiParser;
import com.caiheng.api.dao.product.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderAppraiseServiceImpl extends BaseServiceImpl<OrderAppraiseMapper, OrderAppraise> implements OrderAppraiseService {

    @Resource
    private OrderAppraiseMapper orderAppraiseMapper;
    @Resource
    private OrderAppraiseImgMapper orderAppraiseImgMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductAttrValueMapper productAttrValueMapper;
    @Resource
    private ProductOrderMapper productOrderMapper;
    @Resource
    private ProductMapper productMapper;

    @Value("${web.imgPrefix}")
    private String imgPrefix;//图片地址

    @Override
    public int addAppraiseImg(int appraise_id, String imgUrl, Integer sort) {
        if (appraise_id <= 0){
            return 0;
        }
        if (imgUrl == null || "".equals(imgUrl)){
            return -1;
        }
        OrderAppraiseImg orderAppraiseImg = new OrderAppraiseImg();
        orderAppraiseImg.setAppraiseId(appraise_id);
        orderAppraiseImg.setImgUrl(imgUrl);
        orderAppraiseImg.setSort(sort);
        return orderAppraiseImgMapper.insert(orderAppraiseImg);
    }

    @Override
    public List<OrderAppraise> selectByProductId(Integer productId, Integer pageIndex, Integer pageSize) {
        if (productId<=0){
            return null;
        }
        if (pageIndex<=0){
            pageIndex = 1;
        }
        if (pageSize<=0){
            pageSize = 5;
        }
        int offset = (pageIndex-1)*pageSize;

        List<ProductAttrValue> productSkuList = productAttrValueMapper.selectList(
                new LambdaQueryWrapper<ProductAttrValue>()
                        .select(ProductAttrValue::getId)
                        .eq(ProductAttrValue::getProductId,productId));
        if (productSkuList!=null && productSkuList.size()>0){
            List<OrderAppraise> list = orderAppraiseMapper.selectBySkuIds(productSkuList,offset,pageSize);
            if (list!=null && list.size()>0){
                for (OrderAppraise appraise : list){

                    ProductOrder orders = productOrderMapper.selectById(appraise.getOrderId().longValue());
                    if (orders !=null){
                        User user = userMapper.selectById(orders.getUserId());
                        if (user!=null){
                            if(user.getNickName()!=null){
                                user.setNickName(EmojiParser.parseToUnicode(user.getNickName()));
                            }
                            appraise.setUser(user);
                        }
                    }
                    List<OrderAppraiseImg> list_imgs = orderAppraiseImgMapper.selectListByColumn("appraise_id",appraise.getId());
                    if (list_imgs!=null && list_imgs.size()>0){
                        for (OrderAppraiseImg img : list_imgs){
                            img.setImgUrl(imgPrefix +img.getImgUrl());
                        }
                    }
                    appraise.setImgs(list_imgs);
                }
            }
            return list;
        }
       return null;
    }

    @Override
    public List<OrderAppraise> selectByMerId(int shopId, int pageIndex, int pageSize) {
        if (shopId <= 0){
            return null;
        }
        if (pageIndex <= 0){
            pageIndex = 1;
        }
        if (pageSize <= 0){
            pageSize = 5;
        }
        QueryWrapper<OrderAppraise> qw = new QueryWrapper<>();
        qw.eq("shop_id",shopId);
        qw.orderByDesc("create_time");
        List<OrderAppraise> list = orderAppraiseMapper.getPageEntityList(qw, pageIndex, pageSize);
        if (list != null && list.size() > 0){
            for (OrderAppraise appraise : list){
                ProductOrder orders = productOrderMapper.selectById(appraise.getOrderId());
                if (orders != null){
                    User user = userMapper.selectById(orders.getUserId());
                    if (user!=null){
                        if(user.getNickName()!=null){
                            user.setNickName(EmojiParser.parseToUnicode(user.getNickName()));
                        }
                        appraise.setUser(user);
                    }
                }
                List<OrderAppraiseImg> list_imgs = orderAppraiseImgMapper.selectListByColumn("appraise_id",appraise.getId());
                if (list_imgs != null && list_imgs.size()>0){
                    for (OrderAppraiseImg img : list_imgs){
                        img.setImgUrl(imgPrefix +img.getImgUrl());
                    }
                }
                appraise.setImgs(list_imgs);
            }
        }
        return list;
    }
}
